/** 下载app */
export const clickOpenApp = () => {
    const ua = navigator.userAgent.toLowerCase();
    if (ua.match(/(iphone|ipod|ipad);?/i)) {
      console.log('苹果系统');
      window.location = "https://apps.apple.com/cn/app/topspace/id6444554404"
    } else if (ua.match(/android/i) || ua.match(/vivo/i)) {
      console.log('安卓系统');
      let hasApp = true
      let t = 1000
      const t1 = Date.now()
      const ifr = document.createElement("iframe");
      const url = getDownloadUrl()
      setTimeout(function () {
          if (!hasApp) {
              window.location.href = url
          }
          document.body.removeChild(ifr);
      }, 2000);
      ifr.setAttribute('src', 'changan://topspace/home');
      ifr.setAttribute('style', 'display:none');
      document.body.appendChild(ifr);
      setTimeout(function () { //启动app时间较长处理
          let t2 = Date.now();
          if (t2 - t1 < t + 100) {
              hasApp = false;
          }
      }, t);
    }
}


  const getDownloadUrl = () => {
    const sUserAgent = navigator.userAgent.toLowerCase()
    const isHuawei = sUserAgent.match(/huawei/i) == "huawei";
    const isHonor = sUserAgent.match(/honor/i) == "honor";
    const isOppo = sUserAgent.match(/oppo/i) == "oppo";
    const isOppoR15 = sUserAgent.match(/pacm00/i) == "pacm00";
    const isVivo = sUserAgent.match(/vivo/i) == "vivo";
    const isXiaomi = sUserAgent.match(/mi/i) == "mi";
    const isXiaomi2s = sUserAgent.match(/mix/i) == "mix";
    const isRedmi = sUserAgent.match(/redmi/i) == "redmi";

    if (isHuawei || isHonor) {
        return "appmarket://details?id=cn.com.changan.sdamobile";
    } else if (isOppo || isOppoR15) {
      return "oppomarket://details?packagename=cn.com.changan.sdamobile";
    } else if (isVivo) {
        return "vivomarket://details?id=cn.com.changan.sdamobile";
    } else if (isXiaomi || isRedmi || isXiaomi2s) {
      return "mimarket://details?id=cn.com.changan.sdamobile";
    }else {
      return "market://details?id=cn.com.changan.sdamobile"
    }
} 
